﻿using System.Collections.Generic;

namespace Medicalsystem.MCP.FormDesigner
{
    /// <summary>
    /// 粘贴 复制
    /// </summary>
    public class CopyPaste
    {
        private string _copyXML = string.Empty;
        private ECopyType _copeyType = ECopyType.None;
        private int _copyOffset = 0; //持续粘贴时，有一个不断变化的偏移值

        /// <summary>
        /// 复制类型
        /// </summary>
        public ECopyType CopyType
        {
            get { return _copeyType; }
        }

        /// <summary>
        /// 复制粘贴偏移量
        /// </summary>
        public int CopyOffset
        {
            get { return _copyOffset; }
        }

        /// <summary>
        /// 复制指定控件
        /// </summary>
        public void Copy(List<CtrlMngDesign> ctrls)
        {
            _copyOffset = 7;
            _copyXML = XML.ControlsToXML(ctrls);
            _copeyType = ECopyType.CopyControls;
        }

        /// <summary>
        /// 复制整个模板
        /// </summary>
        /// <param name="model"></param>
        public void Copy(ModelDesign model)
        {
            _copyOffset = 0;
            _copyXML = XML.ModelToXML(model);
            _copeyType = ECopyType.CopyModel;
        }

        /// <summary>
        /// 粘贴整个模板
        /// </summary>
        /// <param name="model"></param>
        public void Paste(ModelDesign model)
        {
            //导入前 先根据ModelXML分析出模板引用的数据，根据引用数据生成实例
            System.Xml.XmlDataDocument doc = new System.Xml.XmlDataDocument();
            doc.LoadXml(_copyXML);

            //DataProvider DP = new DataProvider(model);
            //DP.InitModelReference(doc);

            XML.XMLToModel(_copyXML, model);
        }

        /// <summary>
        /// 粘贴复制控件
        /// </summary>
        /// <param name="container">控件的父模板</param>
        /// <param name="model"></param>
        public List<CtrlMngDesign> Paste(ContainerHandler container, ModelDesign model)
        {
            List<CtrlMngDesign> ctrls = XML.XMLToControls(_copyXML, container, model);
            if (_copeyType == ECopyType.CutControls)
            {
                _copeyType = ECopyType.None;
                _copyXML = string.Empty;
            }
            else
            {
                foreach (CtrlMngDesign ctrl in ctrls)
                {
                    (ctrl.Ctrl as System.Windows.Forms.Control).Top += _copyOffset;
                    (ctrl.Ctrl as System.Windows.Forms.Control).Left += _copyOffset;
                }

                _copyOffset += 7;
            }

            return ctrls;
        }

        /// <summary>
        /// 剪切控件
        /// </summary>
        /// <param name="ctrls"></param>
        public void Cut(List<CtrlMngDesign> ctrls)
        {
            _copyOffset = 0;
            _copyXML = XML.ControlsToXML(ctrls);
            _copeyType = ECopyType.CutControls;
        }
    }

    public enum ECopyType
    {
        /// <summary>
        /// 无操作
        /// </summary>
        None,

        /// <summary>
        /// 复制模板
        /// </summary>
        CopyModel,

        /// <summary>
        /// 复制控件
        /// </summary>
        CopyControls,

        /// <summary>
        /// 剪切控件
        /// </summary>
        CutControls
    }
}